package com.presto.directo.mvvm.view

import android.os.Bundle
import android.view.View
import androidx.core.view.*
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.presto.directo.R
import com.presto.directo.base.BaseActivity
import com.presto.directo.databinding.*
import com.presto.directo.mvvm.viewmodel.*
import com.presto.directo.utils.*
import kotlinx.coroutines.*

@Suppress("DeferredResultUnused")
class MainActivity : BaseActivity() {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    private val viewModel by lazy { getViewModel<MainViewModel>() }
    private val fragments by lazy { arrayListOf(MainFragment(), MineFragment()) }
    private val deviceInfoUtil by lazy { DeviceInfoUtil(this) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val myPagerAdapter = MyPagerAdapter(supportFragmentManager, fragments)
        binding.viewPager.adapter = myPagerAdapter
        binding.rgMain.setOnCheckedChangeListener { group, checkedId ->
            binding.viewPager.setCurrentItem(if (checkedId == R.id.rb_main) 0 else 1, false)
        }
        viewModel.versionLiveData.observe(this) {
            showDialog { dialog ->
                val binding = LayoutCommonDialogBinding.inflate(layoutInflater)
                dialog.setContentView(binding.root)
                binding.tvTitle.isVisible = true
                binding.tvTitle.text = getString(R.string.text_main_update_title)
                binding.tvContent.text = it.comment
                binding.tvCancel.isGone = it.version_status == "1"
                binding.tvCancel.setOnClickListener { dialog.dismiss() }
                binding.tvConfirm.setOnClickListener { _ ->
                    dialog.dismiss()
                    toPlay()
                    if (it.version_status == "1") {
                        exit()
                    }
                }
            }
        }
        start(onNext = {
            withContext(Dispatchers.IO) {
                val gaId = AdvertisingIdClient.getAdvertisingIdInfo(applicationContext).id ?: ""
                SPUtil.put("gaId", gaId)
            }
        }, toast = false)
        if (UserUtil.isLogin()) {
            start(onNext = {
                pushId.forEach {
                    push(this, deviceInfoUtil, it)
                }
            }, toast = false)
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.checkUpdate()
    }

    private var touchBackTime: Long = 0

    override fun onBackPressed() {
        if (System.currentTimeMillis() - touchBackTime >= 2000) {
            toast(R.string.main_exit_click_again)
            val currentTime = System.currentTimeMillis()
            touchBackTime = currentTime
        } else {
            exit()
        }
    }

    override val rootView: View
        get() = binding.root

    override fun isTransparentStatusBar(): Boolean {
        return true
    }

    override fun isDarkStatusBar(): Boolean {
        return true
    }

    override fun onDestroy() {
        super.onDestroy()
        locationUtil.stopLocation()
    }

}